#include "LinkList.h"
#include <iostream>
#include "DynamicArray.h"

LinkList::LinkList():size(0)
{
    head = new Node(0);
}

LinkList::LinkList(const LinkList &list)
{
    size = list.size;
    head = new Node(0);
    Node *tp = head;
    Node *travelPoint = list.head->next;
    while(travelPoint != nullptr)
    {
        Node *newNode = new Node(travelPoint->value);
        tp->next =newNode;
        tp = tp->next;
        travelPoint =travelPoint->next; 
    }
}

LinkList &LinkList::operator=(const LinkList &list)
{
    while(head != nullptr)
    {
        Node *nextNode = head->next;//下一个节点存下来
        delete head;
        head = nextNode;
    }


    size = list.size;
    head = new Node(0);
    Node *tp = head;
    Node *travelPoint = list.head->next;
    while(travelPoint != nullptr)
    {
        Node *newNode = new Node(travelPoint->value);
        tp->next =newNode;
        tp = tp->next;
        travelPoint =travelPoint->next; 
    }


    return *this;
}

LinkList::~LinkList()
{
    while(head != nullptr)
    {
        Node *nextNode = head->next;//下一个节点存下来
        delete head;
        head = nextNode;
    }
}

void LinkList::InsertTail(const ElementType &element)       //尾插法    O(n)
{
    //遍历指针
    Node *travelPoint =head;
    while(travelPoint->next != nullptr) //还没有到尾部
        travelPoint =travelPoint->next;
    Node *newNode = new Node(element);//创建一个新的节点
    travelPoint->next = newNode;
   size++; 
}

void LinkList::InsertHead(const ElementType &element)        //头插法   O(1)
{
    Node *newNode = new Node(element);
    newNode->next =head->next;
    head->next = newNode;
    size++;
}

void LinkList::InsertByIndex(size_t index, const ElementType &element)      //中间插入
{
    if(index > size)
    {
        std::cout<<"位置非法!\n";
        return;
    }
    Node *travelPoint = head;
    for(size_t i=0;i < index;i++)
    {
        travelPoint =travelPoint->next;
    }
    Node *newNode = new Node(element);
    newNode->next=travelPoint->next;
    travelPoint->next = newNode;
    size++;
}

void LinkList::RemoveByIndex(size_t index)            //按位置删除  O(n)
{
    if(index > size)
    {
        std::cout<<"删除位置非法!\n";
        return;
    }
    Node *travelPoint = head;
    for(size_t i=0;i < index; i++)
        travelPoint =travelPoint->next;
    Node *freeNode =travelPoint->next;
    travelPoint->next=freeNode->next;
    delete freeNode;
    size--;

}

void LinkList::RemoveByElement(const ElementType &element)      //按值删除  o(n)
{
    Node *travelPoint = head;
    while(travelPoint->next != nullptr)
    {
        if(travelPoint->next->value == element)
        {
            Node *freeNode = travelPoint->next;
            travelPoint->next=freeNode->next;
            delete freeNode;
            size--;
        }
        else
            travelPoint = travelPoint->next;
    }
}

ElementType *LinkList::FindByIndex(size_t index)    //查找
{
    if(index > size)
        return nullptr;
    Node*travelPoint =head->next;
    for(size_t i=0;i < index; i++)
    {
            travelPoint =travelPoint->next;
    }
    
    return &travelPoint->value;
}

DARRAY::DynamicArray LinkList::FindByElement(const ElementType &element)
{
    DARRAY::DynamicArray array;
    Node *travelPoint = head->next;
    while(travelPoint != nullptr)
    {
        if(travelPoint->value == element)
        {
            array.InsertTail(&travelPoint->value);
        }
        travelPoint =travelPoint->next;
    }
    return array;
}

void LinkList::UpdateByIndex(size_t index, const ElementType &element)      //按位置修改
{
     if(index > size)
    {
        std::cout<<"位置非法!\n";
        return;
    }
    Node *travelPoint = head->next;
    for(size_t i=0; i<index; i++)
    {
        travelPoint =travelPoint->next;
    }      
    travelPoint->value=element;
}

void LinkList::UpdateByElement(const ElementType &oldValue, const ElementType &newValue)    //按值修改
{
    Node *travelPoint = head->next;
    while(travelPoint != nullptr)
    {
        if(travelPoint->value == oldValue)
        {
            travelPoint->value = newValue;
        }
        travelPoint=travelPoint->next;
    }
}

void LinkList::BubbleSort()         //冒泡排序 
{
    for(size_t i=0;i<size-1;i++)
    {
        Node *travelPoint = head;//每次循环都回到链表的开头
        for(size_t j=0;j<size-i-1;j++)
        {
            if(travelPoint->next->value > travelPoint->next->next->value)
            {
                Node *prev = travelPoint->next;
                Node *back = prev->next;
                prev->next = back->next;
                back->next = prev;
                travelPoint->next = back;
            }
            travelPoint = travelPoint->next;
        }
    }
}

void LinkList::Sort()
{
    Fastsort(head->next,nullptr);
}

ElementType *LinkList::GetReserveIndex(size_t index)
{
    return nullptr;
}

LinkList LinkList::Merge(const LinkList &list)      //归并
{
    LinkList result;
    Node *first = head->next;
    Node* second = list.head->next;
    while( first && second)
    {
        if(first->value< second-> value)
        {
            result.InsertTail(first->value);first = first->next;
            first =first->next;
        }
        else
        {
            result.InsertTail(second->value);
            second = second->next;
        }

    }

    while(first)
    {
        result.InsertTail(first->value);
        first = first->next;
    }

    while(second)
    {
        result.InsertTail(second->value);
        second =second->next;
    }

    return result;
}

void LinkList::Reserve()
{
    Node *prev = nullptr;
    Node *cur = head->next;
    Node *Next = cur->next;
    while(Next != nullptr)
    {
        cur->next = prev;//逆序
        prev = cur;
        cur = Next;
        Next = cur->next;
    }
    cur->next = prev;//最后一个节点单独逆序
    head->next = cur;
}

void LinkList::Reserve2()
{
    head->next =reserve(head->next);
}

void LinkList::show()
{
    std::cout<<"size : "<<size<<std::endl;
    Node *travelPoint = head->next;
    while(travelPoint != nullptr)//链表还没有结束
    {
        std::cout<<travelPoint->value<<" ";
        travelPoint = travelPoint->next;//遍历指针往后走
    }
    std::cout<<std::endl;
}

void LinkList::Fastsort(Node *start, Node *end) 
{
    //终止条件
    if(start == end)
        return;
    Node *temp = start;//基准值指针
    Node *slow= start;//慢指针
    Node *fast= start->next;//快指针
    while(fast != end)//快指针没有到结尾
    {
        //快指针指向的值比基准值小
        if(fast->value < temp->value)
        {
            slow = slow->next;
            std::swap(slow->value,fast->value);//交换快慢指针的值
        }
        fast = fast->next;
    }
    std::swap(temp->value,slow->value);//将基准值归位
    Fastsort(start,slow);       //[start,slow)
    Fastsort(slow->next,end);
}

LinkList::Node *LinkList::reserve(Node *node)
{
    //返回值是原来的尾巴 新链表的头
    if(node->next == nullptr)//这个节点是尾巴 逆序完的头
        return node;
    else
    {
        Node *newHead = reserve(node->next);//递归往后走
        node->next->next = node;     //逆序核心
        node->next = nullptr;        //新尾巴  原来的头  指针指向空
        return newHead;
    }

}
